package com.gree.ssm.web.admin;

import com.gree.ssm.bean.BlogType;
import com.gree.ssm.bean.PageBean;
import com.gree.ssm.common.CommonResult;
import com.gree.ssm.service.BlogTypeService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author :xiaossong
 * @Description: 管理员博客类别Controller层
 * @Date Created in 下午11:18 2017/10/15
 */
@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {

    @Resource
    private BlogTypeService blogTypeService;




    /**
     * 分页查询博客类别信息
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> list(@RequestParam(value="page",required=false)String page, @RequestParam(value="rows",required=false)String rows)throws Exception{
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<BlogType> blogTypeList=blogTypeService.list(map);
        Long total=blogTypeService.getTotal(map);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("rows",blogTypeList);
        result.put("total",total);
        return result;

    }






    /**
     * 添加或者修改博客类别信息
     * @param blogType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/save")
    public CommonResult save(BlogType blogType)throws Exception{
        int resultTotal=0; // 操作的记录条数
        if(blogType.getId()==null){
            resultTotal=blogTypeService.add(blogType);
        }else{
            resultTotal=blogTypeService.update(blogType);
        }

        if(resultTotal>0){

            return new CommonResult(1001,"success");
            //result.put("success", true);
        }else{
            //result.put("success", false);
            return new CommonResult(1002,"error");
        }
       // System.out.printf(result.toString());
       // ResponseUtil.write(response, result);
    }
    /**
     * 删除博客类别信息
     * @param ids
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/delete")
    public CommonResult delete(@RequestParam(value="ids")String ids)throws Exception{
        String []idsStr=ids.split(",");
        for(int i=0;i<idsStr.length;i++){
            blogTypeService.delete(Integer.parseInt(idsStr[i]));
        }

        return new CommonResult(1003,"success");
    }


}
